package service

import (
	"storeManageApi/models/dbModels"
	"storeManageApi/models/requestModels"
	"storeManageApi/models/requestModels/request_manage_models"
	"storeManageApi/models/responseModels/response_manage_models"
	"storeManageApi/repo"
	"storeManageApi/utils"
)

// ImanageService
//
//	@Description: 管理员接口
type ImanageService interface {
	GetGoodsConfigPage(query requestModels.QueryNoteList) ([]response_manage_models.GoodsConfigsVM, error)
	GetStoreNotesPage(query requestModels.QueryNoteList) ([]response_manage_models.StoreNotesVM, error)
	GetInNotesPage(query requestModels.QueryNoteList) ([]response_manage_models.InNotesVM, error)
	GetOutNotesPage(query requestModels.QueryNoteList) ([]response_manage_models.OutNotesVM, error)
	GetAllGoodsOptions() ([]response_manage_models.GoodsOptionVM, error)
	GetStoreCount() int
	GetGoodsConfigCount() int
	GetInNotesCount() int
	GetOutNotesCount() int
	AddOutNotes(note request_manage_models.NewOutNotes) (request_manage_models.NewOutNotes, error)
	AddInNOtes(note request_manage_models.NewInNotes) (request_manage_models.NewInNotes, error)
	AddGoodsConfig(goods request_manage_models.NewGoodsConfig) (request_manage_models.NewGoodsConfig, error)
}

// CreateManageService
//
//	@Description: 创建服务
//	@return: ImanageService
func CreateManageService() ImanageService {
	var goodsConfigRepo repo.IGoodsConfigRepo = repo.CreateGoodsConfigRepo()
	var storeNotesRepo repo.IStoreNotesRepo = repo.CreateStoreNotesRepo()
	var outNotesRepo repo.IOutNotesRepo = repo.CreateOutNotesRepo()
	var inNotesRepo repo.IInNotesRepo = repo.CreateInNotesRepo()
	return &ManageService{
		GoodsConfigRepo: goodsConfigRepo,
		StoreNotesRepo:  storeNotesRepo,
		OutNotesRepo:    outNotesRepo,
		InNotesRepo:     inNotesRepo,
	}
}

// ManageService
//
//	@Description:
type ManageService struct {
	GoodsConfigRepo repo.IGoodsConfigRepo
	StoreNotesRepo  repo.IStoreNotesRepo
	OutNotesRepo    repo.IOutNotesRepo
	InNotesRepo     repo.IInNotesRepo
}

func (service ManageService) GetAllGoodsOptions() ([]response_manage_models.GoodsOptionVM, error) {
	options, optErr := service.GoodsConfigRepo.GetAll()
	if optErr != nil {
		return nil, optErr
	}
	var results []response_manage_models.GoodsOptionVM = make([]response_manage_models.GoodsOptionVM, 0)
	for _, option := range options {
		results = append(results, response_manage_models.GoodsOptionVM{
			Name: option.Name,
			Uid:  option.Base.Uid,
		})
	}
	return results, nil
}

func (service ManageService) GetOutNotesPage(query requestModels.QueryNoteList) ([]response_manage_models.OutNotesVM, error) {
	outNotes, outErr := service.OutNotesRepo.GetPage(query.Start, query.PageSize)
	if outErr != nil {
		return nil, outErr
	}
	var results []response_manage_models.OutNotesVM = make([]response_manage_models.OutNotesVM, 0)

	for _, note := range outNotes {
		goods, goodsErr := service.GoodsConfigRepo.GetGoodsConfigByUid(note.GoodsUid)
		if goodsErr != nil {
			return nil, goodsErr
		}
		results = append(results, response_manage_models.OutNotesVM{
			GoodsName:      goods.Name,
			Uid:            note.Base.Uid,
			ActionNumber:   note.ActionNumber,
			ActionUserName: note.ActionUserName,
			UseTo:          note.UseTo,
			UseUserName:    note.UseUserName,
			EndCount:       note.EndCount,
			Description:    note.Base.Description,
			Date:           note.Base.Updated,
		})
	}
	return results, nil
}
func (service ManageService) GetOutNotesCount() int {
	count, err := service.OutNotesRepo.GetCount()
	if err != nil {
		return 0
	}
	return int(count)
}

func (service ManageService) GetInNotesCount() int {
	count, err := service.InNotesRepo.GetCount()
	if err != nil {
		return 0
	}
	return int(count)
}

func (service ManageService) GetGoodsConfigCount() int {
	count, err := service.GoodsConfigRepo.GetCount()
	if err != nil {
		return 0
	}
	return int(count)
}

func (service ManageService) GetStoreCount() int {
	count, err := service.StoreNotesRepo.GetCount()
	if err != nil {
		return 0
	}
	return int(count)
}

func (service ManageService) GetInNotesPage(query requestModels.QueryNoteList) ([]response_manage_models.InNotesVM, error) {

	inNotes, inErr := service.InNotesRepo.GetPage(query.Start, query.PageSize)
	if inErr != nil {
		return nil, inErr
	}
	var results []response_manage_models.InNotesVM = make([]response_manage_models.InNotesVM, 0)

	for _, note := range inNotes {
		goods, goodsErr := service.GoodsConfigRepo.GetGoodsConfigByUid(note.GoodsUid)
		if goodsErr != nil {
			return nil, goodsErr
		}
		results = append(results, response_manage_models.InNotesVM{
			GoodsName:      goods.Name,
			Uid:            note.Base.Uid,
			ActionNumber:   note.ActionNumber,
			ActionUserName: note.ActionUserName,
			ManageUserName: note.ManageUserName,
			EndCount:       note.EndCount,
			Description:    note.Base.Description,
			Date:           note.Base.Updated,
			Unit:           goods.Unit,
		})
	}
	return results, nil
}

func (service ManageService) AddInNOtes(note request_manage_models.NewInNotes) (request_manage_models.NewInNotes, error) {
	_, err := service.InNotesRepo.AddRow(note.ConvertInNotes())
	if err != nil {
		return request_manage_models.NewInNotes{}, err
	}
	return note, nil
}

// AddGoodsConfig
//
//	@Description: 添加一条物品配置
//	@receiver: service
//	@param: goods
//	@return: request_manage_models.NewGoodsConfig
//	@return: error
func (service ManageService) AddGoodsConfig(goods request_manage_models.NewGoodsConfig) (request_manage_models.NewGoodsConfig, error) {
	var goodsConfig dbModels.GoodsConfigs = goods.ConvertGoodsConfig()
	_, addGoodsErr := service.GoodsConfigRepo.AddRow(goodsConfig)
	if addGoodsErr != nil {
		return request_manage_models.NewGoodsConfig{}, addGoodsErr
	}
	if goods.StartNumber != 0 {
		var status int = 2
		if goodsConfig.StartNumber <= goodsConfig.SafeNumber {
			status = 1
		}
		if goodsConfig.StartNumber <= 0 {
			status = -1
		}
		var store dbModels.StoreNotes = dbModels.StoreNotes{
			Base: dbModels.Base{
				Uid:         utils.RandomUid(),
				Description: goods.Description,
			},
			GoodsUid:   goodsConfig.Base.Uid,
			SafeNumber: goodsConfig.SafeNumber,
			Count:      goodsConfig.StartNumber,
			Status:     status,
		}
		_, addStoreErr := service.StoreNotesRepo.AddRow(store)
		if addStoreErr != nil {
			return request_manage_models.NewGoodsConfig{}, addGoodsErr
		}
	}
	return goods, nil
}

// AddOutNotes
//
//	@Description: 添加一条出库记录
//	@receiver: service
//	@param: note
//	@return: request_manage_models.NewInNotes
//	@return: error
func (service ManageService) AddOutNotes(note request_manage_models.NewOutNotes) (request_manage_models.NewOutNotes, error) {
	_, err := service.OutNotesRepo.AddRow(note.ConvertOutNotes())
	if err != nil {
		return request_manage_models.NewOutNotes{}, err
	}
	return note, nil
}

// GetStoreNotesPage
//
//	@Description: 分页获取库存数据
//	@receiver: service
//	@param: query
//	@return: []dbModels.StoreNotes
//	@return: error
func (service ManageService) GetStoreNotesPage(query requestModels.QueryNoteList) ([]response_manage_models.StoreNotesVM, error) {
	storeResults, storeErr := service.StoreNotesRepo.GetPage(query.Start, query.PageSize)
	if storeErr != nil {
		return []response_manage_models.StoreNotesVM{}, storeErr
	}
	var results []response_manage_models.StoreNotesVM = make([]response_manage_models.StoreNotesVM, 0)
	//  缓存》redis || 本地map
	for _, store := range storeResults {
		goods, goodsErr := service.GoodsConfigRepo.GetGoodsConfigByUid(store.GoodsUid)
		if goodsErr != nil {
			return []response_manage_models.StoreNotesVM{}, goodsErr
		}
		results = append(results, response_manage_models.StoreNotesVM{
			Uid:         store.Base.Uid,
			GoodsUid:    goods.Base.Uid,
			GoodsName:   goods.Name,
			Count:       store.Count,
			Status:      store.Status,
			SafeNumber:  store.SafeNumber,
			Description: store.Base.Description,
			Unit:        goods.Unit,
		})
	}
	return results, nil
}

// GetGoodsConfigPage
//
//	@Description: 分页获取物品配置数据
//	@receiver: service
//	@param: query
//	@return: []dbModels.GoodsConfigs
//	@return: error
func (service ManageService) GetGoodsConfigPage(query requestModels.QueryNoteList) ([]response_manage_models.GoodsConfigsVM, error) {
	goodsConfigResults, goodsConfigErr := service.GoodsConfigRepo.GetPage(query.Start, query.PageSize)
	if goodsConfigErr != nil {
		return []response_manage_models.GoodsConfigsVM{}, goodsConfigErr
	}
	var results []response_manage_models.GoodsConfigsVM = make([]response_manage_models.GoodsConfigsVM, 0)
	for _, goodsConfig := range goodsConfigResults {
		results = append(results, response_manage_models.GoodsConfigsVM{
			Name:        goodsConfig.Name,
			Uid:         goodsConfig.Base.Uid,
			Model:       goodsConfig.Model,
			Unit:        goodsConfig.Unit,
			Type:        goodsConfig.Type,
			Description: goodsConfig.Base.Description,
			SafeNumber:  goodsConfig.SafeNumber,
			StartNumber: goodsConfig.StartNumber,
			Date:        goodsConfig.Base.Updated,
		})
	}

	return results, nil
}
